import random

from src.main.server.controller.vol_info_controller import VolInfoController
from src.main.server.mapper.impl.score_impl import ScoreImpl
from src.main.server.pojo.Result import Result


class ScoreController:

    def __init__(self):
        self.impl = ScoreImpl()

    def all_info(self):
        result = self.impl.all_info()
        return result

    def get_info(self, data):
        data = self.impl.get_info(data)
        return data

    def update(self, date):
        self.impl.update(date)

    def insert(self, data):
        #1.判断数据库是否存在该字段
        count = self.check(data)
        if count >= 1:
            return None
        #无重复,生成一个模拟积分
        score = random.randint(30, 50)
        #插入表
        vol_name = data[0]
        server_name = data[1]
        self.impl.insert((score, vol_name, server_name))
        #更新用户积分
        VolInfoController().add_score((score, vol_name))
        return Result().success()

    def check(self, data):
        """data = (vol_name, server_name)"""
        return self.impl.check(data)[0][0]


if __name__ == '__main__':
    print(ScoreController().get_info(("李四", "社区服务" )))